<template>

  <splitpanes :horizontal="appStore2.device === 'mobile'" class="default-theme">
    <!--左边数据-->
    <pane size="16" class="min-height">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAddOfTicket" v-hasPermi="['scm:scmDefectTicket:add']"
            >新增</el-button>
          </el-col>
        </el-col>
        <right-toolbar :search="false" v-model:showSearch="showSearchOfTicket" @queryTable="getListOfTicket">
        </right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="scmDefectTicketList"  lazy highlight-current-row @current-change="handleCurrentTicket" >
        <el-table-column label="工单" align="left" prop="ticketCode" />
        <el-table-column label="" align="center" class-name="small-padding fixed-width" width="20">
          <template #default="scope">
            <el-button link type="primary" icon="Delete" @click="handleDeleteOfTicket(scope.row)" v-hasPermi="['scm:scmDefectTicket:remove']"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </pane>
    <pane size="84" style="height: 100%;">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              type="primary"
              plain
              icon="Plus"
              @click="handleAdd"
              :disabled="queryParams.ticketId == null"
              v-hasPermi="['scm:scmDefectInfo:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="success"
              plain
              icon="Edit"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['scm:scmDefectInfo:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['scm:scmDefectInfo:remove']"
          >删除</el-button>
        </el-col>
        <!--    <el-col :span="1.5">-->
        <!--      <el-button-->
        <!--          type="warning"-->
        <!--          plain-->
        <!--          icon="Download"-->
        <!--          @click="handleExport"-->
        <!--          v-hasPermi="['scm:scmDefectInfo:export']"-->
        <!--      >导出</el-button>-->
        <!--    </el-col>-->
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :search="false" :columns="columns" showColumnsType="transfer"
                       :linked-id="projectId" :attachment-type="6" :attachment-path="projectCode"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="scmDefectInfoList" @selection-change="handleSelectionChange" :scrollbar-always-on="true" >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="图号" align="center" prop="projectProduct.drawingNumber" fixed width="98"/>
        <el-table-column label="版本" align="center" prop="projectProduct.version" fixed v-if="columns[0].visible" />
        <el-table-column label="内版本" align="center" prop="projectProduct.internalVersion" fixed v-if="columns[1].visible" />
        <el-table-column label="品名" align="center" prop="projectProduct.productName" fixed v-if="columns[2].visible" />
        <!--      <el-table-column label="ID" align="center" prop="id" />-->
        <!--      <el-table-column label="项目id" align="center" prop="projectId" />-->
        <!--      <el-table-column label="产品id" align="center" prop="productId" />-->
        <!--      <el-table-column label="项目产品ID" align="center" prop="projectProductId" />-->
        <!--      <el-table-column label="订单ID" align="center" prop="customerOrderId" />-->
        <el-table-column label="订单编号" align="center" prop="customerOrderCode" width="170"/>
        <el-table-column label="不良日" align="center" prop="defectDate" width="150">
          <template #default="scope">
            <span>{{ parseTime(scope.row.defectDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="scm_defect_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="检查数量" align="center" prop="inspectionCount" />
        <el-table-column label="不良数量" align="center" prop="defectCount" />
        <el-table-column label="不良率(%)" align="center" prop="defectRate" width="90"/>
        <el-table-column label="不良类型" align="center" prop="defectType" />
        <el-table-column label="不良内容" align="center" prop="defectDetails" />
        <el-table-column label="原因分析" align="center" prop="reasonAnalysis" />
        <el-table-column label="RD对策" align="center" prop="countermeasure" />
        <el-table-column label="客户发货" align="center">
          <el-table-column label="日期" align="center" prop="customerOutDate" width="98">
            <template #default="scope">
              <span>{{ parseTime(scope.row.customerOutDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="customerOutQuantity" />
          <el-table-column label="运送方式" align="center" prop="customerOutChannel">
            <template #default="scope">
              <dict-tag :options="scm_transport_channel" :value="scope.row.customerOutChannel"/>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="RD收货" align="center">
          <el-table-column label="日期" align="center" prop="selfInDate" width="98">
            <template #default="scope">
              <span>{{ parseTime(scope.row.selfInDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="selfInQuantity" />
        </el-table-column>
        <el-table-column label="RD发货" align="center">
          <el-table-column label="日期" align="center" prop="selfOutDate" width="98">
            <template #default="scope">
              <span>{{ parseTime(scope.row.selfOutDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="selfOutQuantity" />
          <el-table-column label="运送方式" align="center" prop="selfOutChannel">
            <template #default="scope">
              <dict-tag :options="scm_transport_channel" :value="scope.row.selfOutChannel"/>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="供应商收货" align="center">
          <el-table-column label="日期" align="center" prop="supplierInDate" width="98">
            <template #default="scope">
              <span>{{ parseTime(scope.row.supplierInDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="supplierInQuantity" />
        </el-table-column>
        <el-table-column label="供应商发货" align="center">
          <el-table-column label="日期" align="center" prop="supplierOutDate" width="98">
            <template #default="scope">
              <span>{{ parseTime(scope.row.supplierOutDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="supplierOutQuantity" />
          <el-table-column label="运送方式" align="center" prop="supplierOutChannel">
            <template #default="scope">
              <dict-tag :options="scm_transport_channel" :value="scope.row.supplierOutChannel"/>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="RD收货" align="center">
          <el-table-column label="日期" align="center" prop="selfInDate2" width="98">
            <template #default="scope">
              <span>{{ parseTime(scope.row.selfInDate2, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="selfInQuantity2" />
        </el-table-column>
        <el-table-column label="RD发货" align="center">
          <el-table-column label="日期" align="center" prop="selfOutDate2" width="98">
            <template #default="scope">
              <span>{{ parseTime(scope.row.selfOutDate2, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="selfOutQuantity2" />
          <el-table-column label="运送方式" align="center" prop="selfOutChannel2">
            <template #default="scope">
              <dict-tag :options="scm_transport_channel" :value="scope.row.selfOutChannel2"/>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="客户收货" align="center">
          <el-table-column label="日期" align="center" prop="customerInDate" width="98">
            <template #default="scope">
              <span>{{ parseTime(scope.row.customerInDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="customerInQuantity" />
        </el-table-column>

<!--        <el-table-column label="返品时间" align="center" prop="toCustReturnDate" width="98">-->
<!--          <template #default="scope">-->
<!--            <span>{{ parseTime(scope.row.toCustReturnDate, '{y}-{m}-{d}') }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <!--      <el-table-column label="返品数量对应的时间" align="center" prop="returnQuantityTime" width="180">-->
        <!--        <template #default="scope">-->
        <!--          <span>{{ parseTime(scope.row.returnQuantityTime, '{y}-{m}-{d}') }}</span>-->
        <!--        </template>-->
        <!--      </el-table-column>-->
        <!--      <el-table-column label="返品数量" align="center" prop="returnQuantity" />-->
<!--        <el-table-column label="出货时间" align="center" prop="shipmentQuantityTime" width="98">-->
<!--          <template #default="scope">-->
<!--            <span>{{ parseTime(scope.row.shipmentQuantityTime, '{y}-{m}-{d}') }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column label="出货数量" align="center" prop="shipmentQuantity" />-->
<!--        <el-table-column label="未代品数" align="center" prop="unsubstitutedQuantity" />-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['scm:scmDefectInfo:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['scm:scmDefectInfo:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </pane>
  </splitpanes>





  <!-- 添加或修改不良品信息记录对话框 -->
  <el-dialog :title="title" v-model="open" width="1200px" append-to-body>
    <el-form ref="scmDefectInfoRef" :model="form" :rules="rules" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="图号" prop="projectProductId" style="width: 264.4px" >
            <el-select
                v-model="form.projectProductId" filterable remote reserve-keyword placeholder="请输入项目中的图号"
                :remote-method="remoteMethodOfProjectProducts" @change="handleChangeOfProjectProducts" :loading="loading" >
              <el-option
                  v-for="item in optionsOfProduct" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="品名" prop="projectProduct.productName" >
            <el-input v-model="form.projectProduct.productName" placeholder="" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="版本号" prop="projectProduct.productName">
            <el-input v-model="form.projectProduct.version" placeholder="" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="内版本号" prop="projectProduct.productName">
            <el-input v-model="form.projectProduct.internalVersion" placeholder="" disabled />
          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="订单编号" prop="customerOrderId">
            <el-select v-model="form.customerOrderId" filterable remote reserve-keyword placeholder="请输入订单编号"
                       :remote-method="remoteMethodOfCustomerOrders" @change="handleChangeOfCustomerOrders" :loading="loading" >
              <el-option
                  v-for="item in optionsOfCustomerOrders" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!--          <el-col :span="6">-->
        <!--            <el-form-item label="订单编号" prop="customerOrderCode">-->
        <!--              <el-input v-model="form.customerOrderCode" placeholder="请输入订单编号" />-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <el-col :span="6">
          <el-form-item label="不良日" prop="defectDate">
            <el-date-picker clearable
                            v-model="form.defectDate"
                            type="date"
                            value-format="YYYY-MM-DD"
                            placeholder="请选择不良发生日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option
                  v-for="dict in scm_defect_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="检查数量" prop="inspectionCount">
            <el-input-number :min="0" :controls="false" v-model="form.inspectionCount" placeholder="请输入检查数量" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="不良数量" prop="defectCount">
            <el-input-number :min="0" :controls="false" v-model="form.defectCount" placeholder="请输入不良数量" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="不良率(%)" prop="defectRate">
            <el-input-number :min="0" :controls="false" v-model="form.defectRate" placeholder="请输入不良率(%)" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="不良类型" prop="defectType">
            <el-input v-model="form.defectType" placeholder="请输入不良类型" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="不良内容" prop="defectDetails">
            <el-input v-model="form.defectDetails" placeholder="请输入不良内容描述" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="原因分析" prop="reasonAnalysis">
            <el-input v-model="form.reasonAnalysis" placeholder="请输入RD原因分析" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="RD对策" prop="countermeasure">
            <el-input v-model="form.countermeasure" placeholder="请输入RD对策" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-divider >返品跟踪</el-divider>

      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="客户发货日期" prop="customerOutDate" label-width="110">
            <el-date-picker clearable v-model="form.customerOutDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择客户发货日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="客户发货数量" prop="customerOutQuantity" label-width="110">
            <el-input-number :min="0" :controls="false" v-model="form.customerOutQuantity" placeholder="请输入客户发货数量" />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="运送方式" prop="customerOutChannel">
            <el-select v-model="form.customerOutChannel" placeholder="请选择运送方式">
              <el-option v-for="dict in scm_transport_channel" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="RD接收日期" prop="selfInDate" label-width="110">
            <el-date-picker clearable v-model="form.selfInDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择RD接收日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="RD接收数量" prop="selfInQuantity" label-width="110">
            <el-input-number :min="0" :controls="false" v-model="form.selfInQuantity" placeholder="请输入RD接收数量" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="RD发货日期" prop="selfOutDate" label-width="110">
            <el-date-picker clearable v-model="form.selfOutDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择RD发货日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="RD发货数量" prop="selfOutQuantity" label-width="110">
            <el-input-number :min="0" :controls="false" v-model="form.selfOutQuantity" placeholder="请输入RD发货数量" />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="运送方式" prop="selfOutChannel">
            <el-select v-model="form.selfOutChannel" placeholder="请选择运送方式">
              <el-option v-for="dict in scm_transport_channel" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="供应商接收日期" prop="supplierInDate" label-width="110">
            <el-date-picker clearable v-model="form.supplierInDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择供应商接收日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="供应商接收数量" prop="supplierInQuantity" label-width="110">
            <el-input-number :min="0" :controls="false" v-model="form.supplierInQuantity" placeholder="请输入供应商接收数量" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="供应商发货日期" prop="supplierOutDate" label-width="110">
            <el-date-picker clearable v-model="form.supplierOutDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择供应商发货日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="供应商发货数量" prop="supplierOutQuantity" label-width="110">
            <el-input-number :min="0" :controls="false" v-model="form.supplierOutQuantity" placeholder="请输入供应商发货数量" />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="运送方式" prop="supplierOutChannel">
            <el-select v-model="form.supplierOutChannel" placeholder="请选择运送方式">
              <el-option v-for="dict in scm_transport_channel" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="RD接收日期" prop="selfInDate2" label-width="110">
            <el-date-picker clearable v-model="form.selfInDate2" type="date" value-format="YYYY-MM-DD" placeholder="请选择RD接收日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="RD接收数量" prop="selfInQuantity2" label-width="110">
            <el-input-number :min="0" :controls="false" v-model="form.selfInQuantity2" placeholder="请输入RD接收数量" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="RD发货日期" prop="selfOutDate2" label-width="110">
            <el-date-picker clearable v-model="form.selfOutDate2" type="date" value-format="YYYY-MM-DD" placeholder="请选择RD发货日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="RD发货数量" prop="selfOutQuantity2" label-width="110">
            <el-input-number :min="0" :controls="false" v-model="form.selfOutQuantity2" placeholder="请输入RD发货数量" />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="运送方式" prop="selfOutChannel2">
            <el-select v-model="form.selfOutChannel2" placeholder="请选择运送方式">
              <el-option v-for="dict in scm_transport_channel" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="客户接收日期" prop="customerInDate" label-width="110">
            <el-date-picker clearable v-model="form.customerInDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择客户接收日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="客户接收数量" prop="customerInQuantity" label-width="110">
            <el-input-number :min="0" :controls="false" v-model="form.customerInQuantity" placeholder="请输入客户接收数量" />
          </el-form-item>
        </el-col>
      </el-row>
<!--      <el-row :gutter="24">-->
<!--        <el-col :span="6">-->
<!--          <el-form-item label="返品时间" prop="toCustReturnDate">-->
<!--            <el-date-picker clearable-->
<!--                            v-model="form.toCustReturnDate"-->
<!--                            type="date"-->
<!--                            value-format="YYYY-MM-DD"-->
<!--                            placeholder="请选择RD到HP的返品时间">-->
<!--            </el-date-picker>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--        <el-col :span="6">-->
<!--          <el-form-item label="出货时间" prop="shipmentQuantityTime">-->
<!--            <el-date-picker clearable-->
<!--                            v-model="form.shipmentQuantityTime"-->
<!--                            type="date"-->
<!--                            value-format="YYYY-MM-DD"-->
<!--                            placeholder="请选择出货数量对应的时间">-->
<!--            </el-date-picker>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--        <el-col :span="6">-->
<!--          <el-form-item label="出货数量" prop="shipmentQuantity">-->
<!--            <el-input-number :min="0" :controls="false" v-model="form.shipmentQuantity" placeholder="请输入出货数量" />-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--        <el-col :span="6">-->
<!--          <el-form-item label="未代品数" prop="unsubstitutedQuantity">-->
<!--            <el-input-number :min="0" :controls="false" v-model="form.unsubstitutedQuantity" placeholder="请输入未代品数量" />-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->

      <!--        <el-form-item label="项目id" prop="projectId">-->
      <!--          <el-input v-model="form.projectId" placeholder="请输入项目id" />-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="产品id" prop="productId">-->
      <!--          <el-input v-model="form.productId" placeholder="请输入产品id" />-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="项目产品ID" prop="projectProductId">-->
      <!--          <el-input v-model="form.projectProductId" placeholder="请输入项目产品ID" />-->
      <!--        </el-form-item>-->


      <!--        <el-form-item label="返品数量对应的时间" prop="returnQuantityTime">-->
      <!--          <el-date-picker clearable-->
      <!--            v-model="form.returnQuantityTime"-->
      <!--            type="date"-->
      <!--            value-format="YYYY-MM-DD"-->
      <!--            placeholder="请选择返品数量对应的时间">-->
      <!--          </el-date-picker>-->
      <!--        </el-form-item>-->
      <!--        <el-form-item label="返品数量" prop="returnQuantity">-->
      <!--          <el-input v-model="form.returnQuantity" placeholder="请输入返品数量" />-->
      <!--        </el-form-item>-->

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>


  <!-- 添加或修改售后工单对话框 -->
  <el-dialog :title="titleOfTicket" v-model="openOfTicket" width="500px" append-to-body>
    <el-form ref="scmDefectTicketRef" :model="formOfTicket" :rules="rulesOfTicket" label-width="80px">
      <el-form-item label="客户订单" prop="customerOrderId">
        <el-select v-model="formOfTicket.customerOrderId" placeholder="请输入订单编号"
                    @change="handleChangeOfCustomerOrdersOfTicket"  >
          <el-option
              v-for="item in optionsOfCustomerOrders" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="售后工单ID" prop="ticketCode">-->
<!--        <el-input v-model="formOfTicket.ticketCode" placeholder="请输入售后工单" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="供应商订单ID" prop="supplierOrderId">-->
<!--        <el-input v-model="formOfTicket.supplierOrderId" placeholder="请输入供应商订单ID" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="供应商订单编号" prop="supplierOrderCode">-->
<!--        <el-input v-model="formOfTicket.supplierOrderCode" placeholder="请输入供应商订单编号" />-->
<!--      </el-form-item>-->
      <el-form-item label="工单日期" prop="defectDate">
        <el-date-picker clearable
                        v-model="formOfTicket.defectDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择不良发生日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="formOfTicket.status" placeholder="请选择状态">
          <el-option
              v-for="dict in scm_defect_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="处理人ID" prop="userId">-->
<!--        <el-input v-model="formOfTicket.userId" placeholder="请输入处理人ID" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="处理人" prop="userName">-->
<!--        <el-input v-model="formOfTicket.userName" placeholder="请输入处理人" />-->
<!--      </el-form-item>-->
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitFormOfTicket">确 定</el-button>
        <el-button @click="cancelOfTicket">取 消</el-button>
      </div>
    </template>
  </el-dialog>

</template>

<script setup>
import { listScmDefectInfo, getScmDefectInfo, delScmDefectInfo, addScmDefectInfo, updateScmDefectInfo } from "@/api/scm/scmDefectInfo";
import {listScmProjectProducts} from "@/api/scm/scmProjectProducts.js";
import {listScmCustomerOrders} from "@/api/scm/scmCustomerOrders.js";
import {Pane, Splitpanes} from "splitpanes";
import "splitpanes/dist/splitpanes.css"
import useAppStore from "@/store/modules/app.js";
import {
  addScmDefectTicket,
  delScmDefectTicket,
  listScmDefectTicket,
  updateScmDefectTicket
} from "@/api/scm/scmDefectTicket.js";
const appStore2 = useAppStore()


const props = defineProps({
  projectId: {
    type: String,
    default: ''
  },
  projectCode: {
    type: String,
    default: ''
  }
})

// const data = reactive({
//   form: {},
//   queryParams: {
//     projectId: props.projectId,
//   }
// });

const { proxy } = getCurrentInstance();
const { scm_defect_status, scm_transport_channel } = proxy.useDict('scm_defect_status', 'scm_transport_channel');
const today = computed(() => {
  const date = new Date();
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
});

const scmDefectInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: props.projectId,
    productId: null,
    projectProductId: null,
    customerOrderId: null,
    ticketId: null,
    customerOrderCode: null,
    defectDate: null,
    status: null,
    inspectionCount: null,
    defectCount: null,
    defectRate: null,
    defectType: null,
    defectDetails: null,
    reasonAnalysis: null,
    countermeasure: null,
    toCustReturnDate: null,
    returnQuantityTime: null,
    returnQuantity: null,
    shipmentQuantityTime: null,
    shipmentQuantity: null,
    unsubstitutedQuantity: null,
  },
  rules: {
    projectProductId: [
      { required: true, message: "产品不能为空", trigger: "blur" }
    ],
    customerOrderId: [
      { required: true, message: "订单不能为空", trigger: "blur" }
    ],
    customerOrderCode: [
      { required: true, message: "订单编号不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

// 监听 projectId 变化
watch(
    () => props.projectId,
    (newVal) => {
      console.log('projectId changed to:', newVal);
      queryParams.value.projectId = newVal; // 更新本地变量
      getList();
    }
);

/** 查询不良品信息记录列表 */
function getList() {
  loading.value = true;
  listScmDefectInfo(queryParams.value).then(response => {
    scmDefectInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    projectId: queryParams.value.projectId,
    productId: null,
    projectProductId: null,
    customerOrderId: null,
    customerOrderCode: null,
    ticketId: null,
    supplierOrderId: null,
    supplierOrderCode: null,
    defectDate: today.value,
    status: 1,
    inspectionCount: 0,
    defectCount: 0,
    defectRate: 0,
    defectType: null,
    defectDetails: null,
    reasonAnalysis: null,
    countermeasure: null,
    customerInDate: null,
    customerInQuantity: null,
    customerOutDate: null,
    customerOutQuantity: null,
    customerOutChannel: null,
    selfInDate: null,
    selfInQuantity: null,
    selfOutDate: null,
    selfOutQuantity: null,
    selfOutChannel: null,
    selfInDate2: null,
    selfInQuantity2: null,
    selfOutDate2: null,
    selfOutQuantity2: null,
    selfOutChannel2: null,
    supplierInDate:  null,
    supplierInQuantity: null,
    supplierOutDate: null,
    supplierOutQuantity: null,
    supplierOutChannel: null,
    toCustReturnDate: null,
    returnQuantityTime: null,
    returnQuantity: null,
    shipmentQuantityTime: null,
    shipmentQuantity: null,
    unsubstitutedQuantity: null,
    projectProduct: {
      id: null,
      projectId: null,
      productId: null,
      projectProductStatus: null,
      customerStatus: null,
      supplierStatus: null,
      purchaseCategory: null,
      demandType: null,
      drawingNuber: null,
      version: null,
      internalVersion: null,
      productName:  null,
      material: null,
      inflamingRetardingLevel:  null,
      specification: null,
      model: null,
      color: null,
      drawingComposition: null,
      remark: null
    },
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("scmDefectInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  if ( queryParams.value.ticketId == null){
    proxy.$modal.msgError("请选择售后工单");
    return;
  }
  reset();
  form.value.ticketId = queryParams.value.ticketId;
  form.value.customerOrderId = formOfTicket.value.customerOrderId;
  form.value.customerOrderCode = formOfTicket.value.customerOrderCode;
console.log(form.value.customerorderId);
  open.value = true;
  title.value = "添加不良品信息记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getScmDefectInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改不良品信息记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["scmDefectInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateScmDefectInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addScmDefectInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除不良品信息记录编号为"' + _ids + '"的数据项？').then(function() {
    return delScmDefectInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('scm/scmDefectInfo/export', {
    ...queryParams.value
  }, `scmDefectInfo_${new Date().getTime()}.xlsx`)
}



// 产品 图号 客户下拉框
const optionsOfProduct = ref([]);
const projectProductsList = ref([]);
const queryParamsOfProjectProducts = ref({});

function resetQueryOfProjectProducts() {
  queryParamsOfProjectProducts.value = {
    pageNum: 1,
    pageSize: 100,
    projectId: queryParams.value.projectId,
    projectProductsNo: null,
    drawingNumber: null,
  }
}
function remoteMethodOfProjectProducts(query) {
  console.log('query', query);
  resetQueryOfProjectProducts();
  if (query !== ''){
    queryParamsOfProjectProducts.value.drawingNumber = query.trim();
    listProjectProductsSelect();
  }
}

function handleChangeOfProjectProducts(value) {
  const projectProducts = projectProductsList.value.find(projectProducts => projectProducts.id === value);
  form.value.productId = projectProducts.productId;
  form.value.projectProduct.productName = projectProducts.productName;
  form.value.projectProduct.version = projectProducts.version;
  form.value.projectProduct.internalVersion = projectProducts.internalVersion;
  // form.value.projectProductsContactId = 0;
  // form.value.projectProductsContactName = " ";
}
function listProjectProductsSelect() {
  listScmProjectProducts(queryParamsOfProjectProducts.value).then(response => {
    projectProductsList.value = response.rows;
    optionsOfProduct.value = projectProductsList.value.map(projectProducts => ({
      label: projectProducts.drawingNumber,
      value: projectProducts.id,
    }));
  });
}

// 订单下拉框
const optionsOfCustomerOrders = ref([]);
const customerOrdersList = ref([]);
// const queryParamsOfCustomerOrders = ref({});
const dataOfOrder = reactive({
  queryParamsOfCustomerOrders: {
    pageNum: 1,
    pageSize: 100,
    projectId: queryParams.value.projectId,
    projectProductsNo: null,
    drawingNumber: null,
  }
});

const { queryParamsOfCustomerOrders } = toRefs(dataOfOrder);
function resetQueryOfCustomerOrders() {
  queryParamsOfCustomerOrders.value = {
    pageNum: 1,
    pageSize: 100,
    projectId: queryParams.value.projectId,
    projectProductsNo: null,
    drawingNumber: null,
  }
}
function remoteMethodOfCustomerOrders(query) {
  console.log('query', query);
  resetQueryOfCustomerOrders();
  if (query !== ''){
    // queryParamsOfCustomerOrders.value.drawingNumber = query.trim();
    listCustomerOrdersSelect();
  }
}

function handleChangeOfCustomerOrders(value) {
  form.value.customerOrderCode = customerOrdersList.value.find(customerOrder => customerOrder.id === value).orderCode;
}
function listCustomerOrdersSelect() {
  console.log(queryParamsOfCustomerOrders.value.pageSize)
  listScmCustomerOrders(queryParamsOfCustomerOrders.value).then(response => {
    customerOrdersList.value = response.rows;
    optionsOfCustomerOrders.value = customerOrdersList.value.map(customerOrder => ({
      label: customerOrder.orderCode,
      value: customerOrder.id,
    }));
  });
}

// 列显隐信息
const columns = ref([
  { key: 0, label: `版本`, visible: true },
  { key: 1, label: `内版本`, visible: true },
  { key: 2, label: `品名`, visible: true },
  { key: 3, label: `材质`, visible: true },
  { key: 4, label: `规格`, visible: true },
  { key: 5, label: `颜色`, visible: true },
  { key: 6, label: `单台 用量`, visible: true },
  { key: 7, label: `月度 用量`, visible: true },
  { key: 8, label: `年度 用量`, visible: true },
  { key: 9, label: `一次性`, visible: true },
  { key: 10, label: `一次性试做`, visible: true },
  { key: 11, label: `试做`, visible: true },
  { key: 12, label: `量产`, visible: true },
  { key: 13, label: `希望报价日`, visible: true },
  { key: 14, label: `希望到货日`, visible: true },
  { key: 15, label: `需求资料`, visible: true },
  { key: 16, label: `备注`, visible: true }
]);

//加入ticket
const scmDefectTicketList = ref([]);
const openOfTicket = ref(false);
const loadingOfTicket = ref(true);
const showSearchOfTicket = ref(true);
const idsOfTicket = ref([]);
const singleOfTicket = ref(true);
const multipleOfTicket = ref(true);
const totalOfTicket = ref(0);
const titleOfTicket = ref("");

const dataOfTicket = reactive({
  formOfTicket: {},
  queryParamsOfTicket: {
    pageNum: 1,
    pageSize: 100,
    projectId: queryParams.value.projectId,
    customerOrderId: null,
    customerOrderCode: null,
    ticketId: null,
    supplierOrderId: null,
    supplierOrderCode: null,
    defectDate: null,
    status: null,
    userId: null,
  },
  rulesOfTicket: {
    projectId: [
      { required: true, message: "项目id不能为空", trigger: "blur" }
    ],
  }
});

const { queryParamsOfTicket, formOfTicket, rulesOfTicket } = toRefs(dataOfTicket);

/** 查询售后工单列表 */
function getListOfTicket() {
  loadingOfTicket.value = true;
  listScmDefectTicket(queryParamsOfTicket.value).then(response => {
    // 创建一个默认记录并放在最前面
    const defaultTicket = {
      id: null,
      ticketCode: "全部数据"
    };

    scmDefectTicketList.value = [defaultTicket, ...response.rows];
    totalOfTicket.value = response.total;
    loadingOfTicket.value = false;
  });
}
/** 点击单号 查询该单子下的 具体信息 */
function handleCurrentTicket(val) {
  if (!val) return;
  queryParams.value.ticketId = val.id;
  formOfTicket.value =  val;
  getList();
}
// 取消按钮
function cancelOfTicket() {
  openOfTicket.value = false;
  resetOfTicket();
}

// 表单重置
function resetOfTicket() {
  const today = new Date();
  const formattedDate = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`;
  formOfTicket.value = {
    id: null,
    projectId: queryParams.value.projectId,
    customerOrderId: null,
    customerOrderCode: null,
    ticketId: null,
    supplierOrderId: null,
    supplierOrderCode: null,
    defectDate: formattedDate,
    status: 1,
    userId: null,
    userName: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("scmDefectTicketRef");
}

/** 新增按钮操作 */
function handleAddOfTicket() {
  resetOfTicket();
  openOfTicket.value = true;
  titleOfTicket.value = "添加售后工单";
}

/** 提交按钮 */
function submitFormOfTicket() {
  proxy.$refs["scmDefectTicketRef"].validate(valid => {
    if (valid) {
      if (formOfTicket.value.id != null) {
        updateScmDefectTicket(formOfTicket.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          openOfTicket.value = false;
          getListOfTicket();
        });
      } else {
        addScmDefectTicket(formOfTicket.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          openOfTicket.value = false;
          getListOfTicket();
        });
      }
    }
  });
}

function handleChangeOfCustomerOrdersOfTicket(value) {
  console.log('value', value);
  formOfTicket.value.customerOrderCode = customerOrdersList.value.find(customerOrder => customerOrder.id === value).orderCode;
}

/** 删除按钮操作 */
function handleDeleteOfTicket(row) {
  if (row.id === 0 || row.id === undefined || row.id === null) {
    proxy.$modal.msgError("该数据不允许删除！");
    return;
  }
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除售后工单编号为"' + _ids + '"的数据项？').then(function() {
    return delScmDefectTicket(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
listCustomerOrdersSelect();
getListOfTicket();
</script>